#include "ParticleEmitterBox.h"
#include "include/Random.h"

namespace ISNW
{
namespace scene
{
	s32 ParticleEmitterBox::emitt(u32 now,u32 timeEscaped,Particle*& point)
	{
		u32 particle_num_per_second = os::Random::rand(MinParticlesPerSecond,MaxParticlesPerSecond);

		u32 particle_num = timeEscaped * 0.001f * particle_num_per_second;

		Particles.set_used(particle_num);

		for(u32 i = 0; i < Particles.size(); ++i)
		{
			Particles[i].position.x = os::Random::rand(box.minEdge.x,box.maxEdge.x);
			Particles[i].position.y = os::Random::rand(box.minEdge.y,box.maxEdge.y);
			Particles[i].position.z = os::Random::rand(box.minEdge.z,box.maxEdge.z);

			core::vector3df dir = Direction;
			dir.rotateXYby(os::Random::rand(0,angle),core::vector3df(0,0,0));
			dir.rotateXZby(os::Random::rand(0,angle),core::vector3df(0,0,0));
			dir.rotateYZby(os::Random::rand(0,angle),core::vector3df(0,0,0));

			Particles[i].startVelocity = dir*os::Random::rand(20,100);

			Particles[i].color = os::Random::rand(MinColor.c,MaxColor.c);

			Particles[i].startTime = now;

			Particles[i].endTime = Particles[i].startTime + os::Random::rand(1000,life);
		}

		point = Particles.pointer();

		return particle_num;
	}
} // end namespace scene
} // end namespace ISNW